import { success } from '../common/ResResult'
import { Context } from 'koa'
import { get, post } from '../decorator/reqmethoddecorator'
import { Controller } from '../decorator/controllerdecorator'
import SearchService from '../modules/search/service/SearchService'
import SearchDao from '../modules/search/dao/SearchDao'

@Controller('/searchmodule')
class searchController {
  @post('/addOrUpdateHistoryKeyword')
  async addOrUpdateHistoryKeyword(ctx: Context) {
    const { historykeyword } = ctx.request.body
    const result = await SearchService.addOrUpdateHistoryKeyWord(historykeyword)
    ctx.body = success(result)
  }
  @get('/searchKeywords/:key')
  async searchKeywords(ctx: Context) {
    const { key } = ctx.params
    const keyWordList = await SearchService.searchKeyWords(key)
    ctx.body = success(keyWordList)
  }
  @get('/getHistoryKeyWordsList')
  async getHistoryKeyWordsList(ctx: Context) {
    const historyKeyWordsList = await SearchService.getHistoryKeyWordsList()
    ctx.body = success(historyKeyWordsList)
  }
  @get('/searchHistoryKeyWords/:historykeyword')
  async searchHistoryKeyWords(ctx: Context) {
    const { historykeyword } = ctx.params
    const dbHistoryKeyWord = await SearchDao.searchHistoryKeyWords(historykeyword)
    ctx.body = success(dbHistoryKeyWord)
  }
  @get('/searchDecovery')
  async searchDecovery(ctx: Context) {
    const historyKeyWordList = await SearchService.searchDecovery()
    ctx.body = success(historyKeyWordList)
  }
  @post('/deleteHistoryKeyWord')
  async deleteHistoryKeyWord(ctx: Context) {
    const { id } = ctx.request.body
    const historyKeyWordList = await SearchService.deleteHistoryKeyWord(id)
    ctx.body = success(historyKeyWordList)
  }
}
